<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/8
 * Time: 10:16
 */

namespace Modules\Admin\Service;
use HiCommon\Repository\RoadRescueRepository;
use HiCommon\Repository\BannerRepository;

class RoadRescueService
{





    /**
     *  获取道路救援数据
     *  hinq 2019 11 08
     *
     **/
    public function get_list( array $arr_search){
        try{
            $arr_option['where'] = ['status'=>1];
            $arr_option['order'] = ['key'=>'sort','value'=>'asc'];
            $arr_list = RoadRescueRepository::get_list( (array) $arr_option );
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);
            return hello_success('成功', $arr_list);
        }catch (\Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  验证表单数据
     *  hinq 2019 11 08
     *
     **/
    public function checkData( array $arr_input ){
        if( empty( $arr_input['name'] ) ){
            return hello_error('名称不能为空');
        }
        if( mb_strlen( $arr_input['name'] ) >= 10 ){
            return hello_error('名称不能超过10字符');
        }

        if( empty( $arr_input['id'] ) ) {
            $where = ['name' => $arr_input['name']];
            $count = RoadRescueRepository::get_count_by_where((array)$where);
            if ($count > 0) {
                return hello_error('名称已存在');
            }
        }

        if( empty( $arr_input['phone'] ) ){
            return hello_error('手机号码不能为空');
        }
//        if( !preg_match("/^1[34578]\d{9}$/", $arr_input['phone'] ) ){
//            return hello_error('手机号码格式不能为空');
//        }
        if( empty( $arr_input['color'] ) ){
            return hello_error('背景颜色不能为空');
        }
        if( empty( $arr_input['type'] ) ){
            return hello_error('请设置类型');
        }
        return $arr_input;
    }

    /**
     *  添加道路救援数据
     *  hinq 2019 11 08
     *
     **/
    public function add_one( array $arr_input ){
        try{
            $data = self::checkData( (array) $arr_input );
            if( $data['state'] == 1){
                return $data;
            }
            $temp['name'] = $data['name'];      //救援名称
            $temp['phone'] = $data['phone'];    //救援电话
            $temp['color'] = $data['color'];    //背景颜色
            $temp['type'] = $data['type'];      //显示类型
            $temp['status'] = 1;
            if( empty( $data['type'] ) ){
                $temp['sort'] = 100;
            }else{
                $temp['sort'] = $data['sort'];
            }
            $res = RoadRescueRepository::add_one( (array) $temp );
            if( empty($res) ){
                return hello_error('添加失败');
            }
            return hello_success('添加成功');
        }catch (\Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  获取指定的道路救援数据
     *  hinq 2019 11 08
     *
     **/
    public function get_one( int $nId ){
        try{
            if( empty( $nId ) ){
                return hello_error('参数错误');
            }
            $arr_info = RoadRescueRepository::get_one((int) $nId);
            if( empty( $arr_info->first() ) ){
                return hello_error('参数错误');
            }
            return $arr_info->toArray();
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  更新指定的道路救援数据
     *  hinq 2019 11 08
     *
     **/
    public function edit_one( array $arr_input ){
        try{
            $data = self::checkData( (array) $arr_input);
            if( $data['state'] == 1 ){
                return $data;
            }
            $temp['id'] = $data['id'];
            $temp['name'] = $data['name'];
            $temp['phone'] = $data['phone'];
            $temp['color'] = $data['color'];
            $temp['type'] = $data['type'];
            $temp['sort'] = $data['sort'];
            $res = RoadRescueRepository::edit_one( (array) $temp );
            if( empty( $res ) ){
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功');
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  删除指定数据
     *  hinq 2019 11 08
     *
     **/
    public function delete_one( int $nId ){
        try{
            if( empty( $nId ) ){
                return hello_error('参数错误');
            }
            $res = RoadRescueRepository::delete_one( (int) $nId );
            if( empty( $res ) ){
                return hello_error('删除失败');
            }
            return hello_success('删除成功');
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }





    /**
     *  获取banner数据
     *  hinq 2019 11 08
     *
     **/
    public function get_list_banner( array $arr_search){
        try{
            $arr_option['where'] = ['type'=> $arr_search['type'], 'status'=>1];
            $arr_option['order'] = ['key'=>'sort','value'=>'asc'];
            $arr_list = BannerRepository::get_list( (array) $arr_search , (array) $arr_option );
            $arr_list->appends($arr_search);//添加链接参数
            $arr_list = list_page_data($arr_list, $arr_search);
            foreach ( $arr_list['data'] as $key => &$value ){
                $value['img_msg'] = oss_full_url( $value['img']);
            }
            return hello_success('成功', $arr_list);
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  验证banner数据
     *  hinq 2019 11 08
     *
     **/
    public function checkDataBanner( array $arr_input ){
        if( empty( $arr_input['title'] ) ){
            return hello_error('请输入名称');
        }
        if( empty( $arr_input['img'] ) ){
            return hello_error('请上传图片');
        }
        if( $arr_input['type'] ==  3 ){
            if( empty( $arr_input['link_url'] ) ){
                return hello_error('请输入联系方式');
            }
        }else{
            $arr_input['link_url'] = "#";
        }
        if( empty( $arr_input['sort'] ) ){
            $arr_input['sort'] = 100;
        }
        return $arr_input;
    }


    /**
     *  添加banner数据
     *  hinq 2019 11 08
     *
     **/
    public function store_banner( array $arr_input ){
        try{
            $data = self::checkDataBanner( (array) $arr_input );
            if( $data['state'] == 1 ){
                return $data;
            }
            $temp['title'] = $data['title'];
            $temp['img'] = $data['img'];
            $temp['type'] = $data['type'];
            $temp['link_url'] = $data['link_url'];
            $temp['sort'] = $data['sort'];
            $temp['status'] = 1;
            $res = BannerRepository::add_one( (array) $temp );
            if( empty( $res ) ){
                return hello_error('添加失败');
            }
            return hello_success('添加成功');
        }catch ( \Exception $exception){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  编辑获取指定数据
     *  hinq 2019 12 19
     *
     **/
    public function edit_banner( int $nId ){
        try{
            if( empty( $nId ) ){
                return hello_error('参数错误');
            }
            $arr_list = BannerRepository::get_one( (int) $nId );
            if( empty( $arr_list->first() ) ){
                return hello_error('参数错误');
            }
            return hello_success('ok',$arr_list->toarray());
        }catch (\Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  编辑banner数据
     *  hinq 2019 12 19
     *
     **/
    public function update_banner( array $arr_input ){
        try{
            $temp = self::checkDataBanner( (array) $arr_input );
            $data['title'] = $temp['title'];
            $data['img'] = $temp['img'];
            $data['link_url'] = $temp['link_url'];
            $data['id'] = $temp['id'];
            $data['sort'] = $temp['sort'];
            $res = BannerRepository::edit_one( (array) $data );
            if( empty( $res ) ){
                return hello_error('编辑失败');
            }
            return hello_success('编辑成功');
        }catch (\Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  删除指定的banner数据
     *  hinq 2019 12 20
     *
     **/
    public function delete_banner( int $nId ){
       try{
            if( empty( $nId ) ){
                return hello_error('');
            }
           $res = BannerRepository::delete_one( (int) $nId  , (bool) true );
           if( empty( $res ) ){
               return hello_error('删除失败');
           }
           return hello_success('删除成功');
       }catch (\Exception $exception){
           return ['state'=>1,'msg'=>$exception->getMessage()];
       }
    }

















}